package com.example.work01.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.work01.entity.Diary;
import com.example.work01.service.DiaryService;
import com.example.work01.vo.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 日记表 前端控制器
 * </p>
 *
 * @author myb
 * @since 2023-10-01
 */
@Api(tags = "日记管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/diary")
public class DiaryController {
    private final DiaryService diaryService;

    @ApiOperation("查询日记")
    @GetMapping
    public AjaxResult find(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "2") Integer pageSize,
                           Diary diary) {
        return AjaxResult.success(diaryService.findPage(pageNum, pageSize, diary));
    }

    @ApiOperation("根据id查询日记")
    @GetMapping("/{id}")
    public AjaxResult findById(@PathVariable Long id) {
        return AjaxResult.success(diaryService.getById(id));
    }

    /**
     * 添加日记
     * @param diary
     * @return
     */
    @ApiOperation("添加日记")
    @PostMapping
    public AjaxResult add(@RequestBody Diary diary) {
        diaryService.save(diary);
        return AjaxResult.success(diary);
    }

    /**
     * 修改日记
     * @param diary
     * @return
     */
    @ApiOperation("修改日记")
    @PutMapping
    public AjaxResult modify(@RequestBody Diary diary) {
        diaryService.updateById(diary);
        return AjaxResult.success();
    }

    /**
     * 删除日记
     * @param id
     * @return
     */
    @ApiOperation("删除日记")
    @DeleteMapping("/{id}")
    public AjaxResult modify(@PathVariable Long id) {
        diaryService.removeById(id);
        return AjaxResult.success();
    }
}
